package com.hdcircles.autoform.ui.label.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hdcircles.autoform.R;
import com.hdcircles.autoform.model.bean.FormBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hdcircles on 2016/11/21.
 */

public class SharedFormAdapter extends RecyclerView.Adapter<SharedFormAdapter.ViewHolder>{

    private List<FormBean> formBeanList;

    private onItemClickListener onItemClickListener;

    private getUserNameListener userNameListener;

    private onFillClickListener onFillClickListener;

    private onViewClickListener onViewClickListener;


    public void setFormBeanList(List<FormBean> formBeanList) {
        this.formBeanList = formBeanList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_form_shared,parent,false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final FormBean formBean=formBeanList.get(position);
        holder.tvName.setText(formBean.getFormName());
//        holder.tvCreator.setText(formBean.getCreator());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.onItemClick(formBean,view);
            }
        });
        userNameListener.getUser(formBean.getCreator(),holder.tvCreator);
        holder.tvFill.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onFillClickListener.onFill(formBeanList.get(holder.getAdapterPosition()),view);
            }
        });

        holder.tvView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FormBean f=formBeanList.get(holder.getAdapterPosition());
                onViewClickListener.onView(f.getFormId(),f.getReceiver(),view);
            }
        });
    }

    @Override
    public int getItemCount() {
        return null==formBeanList?0:formBeanList.size();
    }

    public void setOnItemClickListener(SharedFormAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setUserNameListener(getUserNameListener userNameListener) {
        this.userNameListener = userNameListener;
    }

    public void setOnFillClickListener(SharedFormAdapter.onFillClickListener onFillClickListener) {
        this.onFillClickListener = onFillClickListener;
    }

    public void setOnViewClickListener(SharedFormAdapter.onViewClickListener onViewClickListener) {
        this.onViewClickListener = onViewClickListener;
    }

    public interface onItemClickListener{
        void onItemClick(FormBean formBean,View view);
    }

    public interface getUserNameListener{
        void getUser(String creator,TextView textView);
    }

    public interface onFillClickListener{
        void onFill(FormBean form,View view);
    }

    public interface onViewClickListener{
        void onView(String formId,String creatorEmail, View view);
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.tv_form_name)
        TextView tvName;
        @BindView(R.id.tv_creator)
        TextView tvCreator;
        @BindView(R.id.tv_fill)
        TextView tvFill;
        @BindView(R.id.tv_view)
        TextView tvView;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
